//
//  THMakeMoneyViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/16.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THMakeMoneyViewController.h"

@interface THMakeMoneyViewController ()
@property(nonatomic, strong) UITextField                     *moneyTextFiled;/**<  */
@property(nonatomic, strong) UITextField                     *nameTextFiled;/**<  */
@property(nonatomic, strong) UITextField                     *accountTextFiled;/**<  */
@end

@implementation THMakeMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    [self stoneobs_ui_configSubView];
    [self config_table_footer];
    [self stoneobs_fun_st_autoAdjustAllResponder];
    // Do any additional setup after loading the view.
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    header.backgroundColor = UIColor.whiteColor;
    CGFloat top = 10;
    
    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(15, top, UISCREEN_WIDTH, 30)
                                                                  title:@"提现金额"
                                                             titleColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                              titleFont:14
                                                           cornerRadius:0
                                                        backgroundColor:nil
                                                        backgroundImage:nil
                                                                  image:nil];
        [header addSubview:stoneobs_ui_button];
        top = stoneobs_ui_button.bottom;
    });
    
    ({
        UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(15, top, UISCREEN_WIDTH - 30, 80)];
        textFiled.keyboardType = UIKeyboardTypeNumberPad;
        textFiled.placeholder = @"请输入提现金额";
        [header addSubview:textFiled];
        [textFiled stoneobs_fun_st_showBottomLine];
        textFiled.font = [UIFont systemFontOfSize:30];
        
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 40, 80)
                                                                text:@"￥"
                                                           textColor:UIColor.blackColor
                                                                font:23
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        
        [textFiled stoneobs_fun_setstoneobs_fun_st_leftView:stoneobs_ui_label];
        self.moneyTextFiled = textFiled;
        top = textFiled.bottom;
    });
    
    ({
        THUserModel * userModel =  STTools_UserManger.defult.stoneobs_fun_get_loginedUser;
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15,top, 300, 30)
                                                                text:[NSString stringWithFormat:@"当前可提供金额%0.2f",userModel.money.floatValue]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:14
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [header addSubview:stoneobs_ui_label];
        top = stoneobs_ui_label.bottom + 10;
    });
    
    ({
        UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH, 14)];
        stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
        [header addSubview:stoneobs_ui_line];
        top = stoneobs_ui_line.bottom;
    });
    
    ({
        UITextField * textFiled = [self textFiledName:@"真实姓名"];
        textFiled.top = top;
        top = textFiled.bottom;
        [header addSubview:textFiled];
        self.nameTextFiled = textFiled;
        [textFiled stoneobs_fun_st_showBottomLine];
    });
    ({
        UITextField * textFiled = [self textFiledName:@"支付宝"];
        textFiled.top = top;
        top = textFiled.bottom;
        [header addSubview:textFiled];
        self.accountTextFiled = textFiled;
        [textFiled stoneobs_fun_st_showBottomLine];
    });

    header.height = top + 10;
    self.tableView.tableHeaderView = header;
}
- (void)config_table_footer{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 100)];
    
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(20, 0, UISCREEN_WIDTH - 40, 44)
                                                  title:@"提交"
                                             titleColor:UIColor.whiteColor
                                              titleFont:15
                                           cornerRadius:18
                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.centerX = self.tableView.width / 2;
    button.centerY = footer.height /2;
    [footer addSubview:button];
    [button setClicAction:^(UIButton *sender) {
        
        if (weakSelf.accountTextFiled.text.length) {
        }else{
            [SVProgressHUD showInfoWithStatus:weakSelf.accountTextFiled.placeholder];
        }

        if (weakSelf.nameTextFiled.text.length) {
        }else{
            [SVProgressHUD showInfoWithStatus:weakSelf.nameTextFiled.placeholder];
        }
        
        if (weakSelf.accountTextFiled.text.length) {
        }else{
            [SVProgressHUD showInfoWithStatus:weakSelf.accountTextFiled.placeholder];
        }
        
        [weakSelf on_selcted_make_money_button];
    }];
    self.tableView.tableFooterView = footer;
}
- (UITextField*)textFiledName:(NSString*)name{
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, UISCREEN_WIDTH - 20, 50)];
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 85, 80)
                                                            text:name
                                                       textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                            font:15
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    
    [textFiled stoneobs_fun_setstoneobs_fun_st_leftView:stoneobs_ui_label];
    textFiled.placeholder = [NSString stringWithFormat:@"请输入%@",name];
    return textFiled;
}
#pragma mark --stoneobs_net netWork Method
- (void)on_selcted_make_money_button{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:@"2" forKey:@"type"];
    if (self.accountTextFiled.text.length) {
        [stoneobs_net_param_dic setObject:self.accountTextFiled.text forKey:@"account"];
    }
    if (self.nameTextFiled.text.length) {
        [stoneobs_net_param_dic setObject:self.nameTextFiled.text forKey:@"name"];
    }
    if (self.moneyTextFiled.text.length) {
        [stoneobs_net_param_dic setObject:self.moneyTextFiled.text forKey:@"money"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/funds/cash"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        [SVProgressHUD showSuccessWithStatus:@"申请提现成功,请等待审核"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
@end
